package com.kaka.bus.engine.user.forget;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/10/17.
 * e-mail : ${email}
 * desc :
 */

public class ForgetPresenter extends RxPresenter<ForgetContract.View> implements ForgetContract.Presenter {


    @Inject
    public ForgetPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public boolean sendyzm(String phone, String tokenKey) {

        /**
         * 发送验证码  发送成功时 应该记住当前发送的号码 防止用户更改号码
         */
        mView.showDialog("发送验证码...");

        addSubscribe(mDataManager
                .getApi()
                .getCode(phone,tokenKey,"sendDynamic", Constant.PLATFORM)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.sendSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));

        return true;
    }

    @Override
    public void rePassword(String phone,String pwd,String yzm) {

        mView.showDialog("重置密码中...");

        addSubscribe(mDataManager
                .getApi()
                .updatePassword(phone,pwd,yzm)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.rePasswordSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void isExist(String phone) {
        mView.showDialog("验证手机号码...");

        addSubscribe(mDataManager
                .getApi()
                .phoneIsExist(phone)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.phoneState(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }
}
